package mobile.secure;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import mobile.vo.Result;
import models.User;
import play.cache.Cache;
import play.mvc.Before;
import play.mvc.Catch;
import play.mvc.Controller;

public class MobileApiSecure extends Controller {
	
	private static final Logger logger = LoggerFactory.getLogger(MobileApiSecure.class);
	
	@Before(priority = 0, unless = { "mobile.UserController.login", "mobile.UserController.resetPwd",  "mobile.UserController.register",
			"mobile.UserController.logout", "mobile.UserController.register", "mobile.UserController.sendSmsValidCode" })
	public static void checkLogin(){
		String imei = params.get("imei");
		if(imei==null||"".equals(imei)){
			renderJSON(Result.invalidParamWithMsg("无效的请求"));
		}
		Long uid = (Long) Cache.get(imei);
		if(uid==null){
			renderJSON(Result.unauthorized());
		}
	}
	
	@Catch(MobileApiException.class)
	public static void catchMyException(MobileApiException e){
		logger.info("程序发生错误：{}", e.toString());
		renderJSON(Result.errorWithMsg(e.getMessage()));
	}
	
	@Catch(Exception.class)
	public static void catchException(Exception e){
		logger.info("程序发生错误：{}", e.toString());
		renderJSON(Result.errorWithMsg(e.getMessage()));
	}
	
	
}
